package patrol.mobile.com.mobilepatrol.activity.bridge.offen;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.realm.Realm;
import io.realm.RealmResults;
import patrol.mobile.com.mobilepatrol.PatrolApp;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.bean.BJ;
import patrol.mobile.com.mobilepatrol.bean.QljcxjcBjqsb;
import patrol.mobile.com.mobilepatrol.bean.Qljcxjcb;
import patrol.mobile.com.mobilepatrol.manager.QManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;

import static patrol.mobile.com.mobilepatrol.R.id.et;
import static patrol.mobile.com.mobilepatrol.R.id.tv;

/**
 * Created by apple on 17/10/5.
 */

public class BridgeOffenCheckBrokenDetailActivity extends BaseActivity{
    @BindView(R.id.item1)
    RelativeLayout rl1;

    @BindView(R.id.item2)
    RelativeLayout rl2;
    @BindView(R.id.item4)
    RelativeLayout rl4;

    @BindView(R.id.item3)
    RelativeLayout rl3;

    private EditText et2;
    private EditText et3;
    private EditText et4;

    @BindView(R.id.bt)
    Button bt;
    //
    private String qljcxjcid;
    //一个桥梁
    private String qlcode;
    private Qljcxjcb qljcxjcb;
    String id;
    private List<QljcxjcBjqsb> mList = new ArrayList<>();
    /** popup窗口里的ListView */
    private ListView mTypeLv;
    /** popup窗口 */
    private PopupWindow typeSelectPopup;

    /** 模拟的假数据 */
    private List<BJ> bjdata;
    private List<String> bjndata;

    /** 数据适配器 */
    private ArrayAdapter<String> dataAdapter;
    private TextView tv_bj;
    private String[] bbjc;
    private String bjcode="";
    private String bjname="";

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_offen_qs_detail;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
//        initImagePicker();
    }

    @Override
    public void initView() {
        super.initView();


        TextView tv1 = rl1.findViewById(tv);
        tv1.setText("部件");
        tv_bj = rl1.findViewById(R.id.tv1);

        TextView tv2 = rl2.findViewById(tv);
        tv2.setText("缺损类型");
        et2 = rl2.findViewById(et);

        TextView tv4 = rl4.findViewById(tv);
        tv4.setText("缺损范围");
        et4 = rl4.findViewById(et);

        TextView tv3 = rl3.findViewById(tv);
        tv3.setText("保养措施意见");
        et3 = rl3.findViewById(et);
    }

    @Override
    public void initData() {
        super.initData();
        qljcxjcid = getIntent().getStringExtra("qljcxjcid");
        qlcode = getIntent().getStringExtra("qlcode");
         id = getIntent().getStringExtra("id");
        qljcxjcb = QManager.getBridgeJC(qlcode,qljcxjcid);
        mList = qljcxjcb.bjqs;
        if (id!=null&&!id.equals("")){
            QljcxjcBjqsb bjqs = QManager.getBridgeJCbjqs(id);
            bjname = bjqs.bjname;
            bjcode = bjqs.bj;
            tv_bj.setText(bjqs.bjname);
            et2.setText(bjqs.qslx);
            et3.setText(bjqs.bycsyj);
            et4.setText(bjqs.qsfw);
        }

    }

    @Override
    public void setListener() {
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String lx = et2.getText().toString().trim();
                final String fw = et3.getText().toString().trim();
                final String yj = et4.getText().toString().trim();
                if (bjcode.equals("")){
                    showToast("请填写部件");
                    return;
                }
                if (lx.equals("")){
                    showToast("请填写缺损类型");
                    return;
                }
                if (fw.equals("")){
                    showToast("请填写缺损范围");
                    return;
               }
                if (yj.equals("")){
                    showToast("请填写措施意见");
                    return;
                }
                if (id!=null&&!id.equals("")){

                    PatrolApp.realm.executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {
                            RealmResults<QljcxjcBjqsb> br = realm.where(QljcxjcBjqsb.class).equalTo("id",id).findAll();
                            QljcxjcBjqsb qljcxjcBjqsb = br.get(0);
                            qljcxjcBjqsb.bj = bjcode;
                            qljcxjcBjqsb.bjname = bjname;
                            qljcxjcBjqsb.qslx = lx;
                            qljcxjcBjqsb.qsfw = fw;
                            qljcxjcBjqsb.bycsyj = yj;
                        }
                    });
                }else{
                    PatrolApp.realm.executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {

                            QljcxjcBjqsb qljcxjcBjqsb = PatrolApp.realm.createObject(QljcxjcBjqsb.class);

                            qljcxjcBjqsb.id=CommonUtil.getUUID();
                            qljcxjcBjqsb.qljcxjcid = qljcxjcid;
                            qljcxjcBjqsb.bj = bjcode;
                            qljcxjcBjqsb.bjname = bjname;
                            qljcxjcBjqsb.qslx = lx;
                            qljcxjcBjqsb.qsfw = fw;
                            qljcxjcBjqsb.bycsyj = yj;
                            qljcxjcb.bjqs.add(qljcxjcBjqsb);
                            mList = qljcxjcb.bjqs;
                        }
                    });
                }
                QManager.setBridegJCUpState(qljcxjcid,false);
                QManager.setQLUpState(qlcode,false);
                Intent mIntent = new Intent();
                // 设置结果，并进行传送
                setResult(200, mIntent);
                CommonUtil.exitActivityAndBackAnim(BridgeOffenCheckBrokenDetailActivity.this,true);
            }
        });
        rl1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击控件后显示popup窗口
                initSelectPopup();
                // 使用isShowing()检查popup窗口是否在显示状态
                if (typeSelectPopup != null && !typeSelectPopup.isShowing()) {
                    typeSelectPopup.showAsDropDown(tv_bj, 0, 10);
                }
            }
        });
    }
    /**
     * 初始化popup窗口
     */
    private void initSelectPopup() {
        mTypeLv = new ListView(this);
        bjData();
        // 设置适配器
        dataAdapter = new ArrayAdapter<String>(this, R.layout.popup_text_item, bjndata);
        mTypeLv.setAdapter(dataAdapter);

        // 设置ListView点击事件监听
        mTypeLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 在这里获取item数据
                bjname = bjndata.get(position);
                // 把选择的数据展示对应的TextView上
                tv_bj.setText(bjname);
                bjcode = bbjc[position];
                // 选择完后关闭popup窗口
                typeSelectPopup.dismiss();
            }
        });
        typeSelectPopup = new PopupWindow(mTypeLv, tv_bj.getWidth(), ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 取得popup窗口的背景图片
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.bg1);
        typeSelectPopup.setBackgroundDrawable(drawable);
        typeSelectPopup.setFocusable(true);
        typeSelectPopup.setOutsideTouchable(true);
        typeSelectPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // 关闭popup窗口
                typeSelectPopup.dismiss();
            }
        });
    }

    private void bjData() {
        Resources resources = getResources();
        bbjc = resources.getStringArray(R.array.bbjc);
        String[] bbjn = resources.getStringArray(R.array.bbjn);
        bjdata = new ArrayList<>();
        bjndata = new ArrayList<>();
        for (int i = 0; i < bbjc.length; i++) {
            BJ bj = new BJ();
            bj.code = bbjc[i];
            bj.name = bbjn[i];
            bjdata.add(bj);
        }
        for (int i=0;i<bbjc.length;i++){
            bjndata.add(bbjn[i]);
        }

    }
    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                CommonUtil.exitActivityAndBackAnim(BridgeOffenCheckBrokenDetailActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("桥梁经常检查缺损详情");
    }
}
